package com.tongandtang.security;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tongandtang.bg.service.IUserService;
import com.tongandtang.entity.UserDetailsImpl;
import com.tongandtang.utils.FormatCheckUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Reference
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //从数据库中获取用户，及其所拥有的权限
        if(FormatCheckUtil.isEmail(username)){
            return new UserDetailsImpl(userService.getUserByEmail(username),username);
        }else{
            return new UserDetailsImpl(userService.getUserByPhone(username),username);
        }
    }

}
